//
//  AutomaticCollectionCell.swift
//  SwiftTest
//
//  Created by 北京 on 2020/12/9.
//  Copyright © 2020 QiKai. All rights reserved.
//

import UIKit

class AutomaticCollectionCell: UICollectionViewCell {
    
    var lab:UILabel!
    var data:String = ""{
        didSet{
           lab.text = String(data)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.backgroundColor = UIColor.randomColor;
        lab = UILabel.init()
        lab.numberOfLines = 0
        lab.font = .systemFont(ofSize: 17)
        self.contentView.addSubview(lab)
        lab.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.top.equalTo(20)
            make.right.equalTo(-10)
            make.bottom.equalTo(-10)
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        let autoLayoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes)
        let targetSize = CGSize(width: layoutAttributes.frame.width, height: 0)
     
        
        let autoLayoutSize = contentView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: UILayoutPriority.required, verticalFittingPriority: UILayoutPriority.defaultLow)
        
        let autoLayoutFrame = CGRect(origin: autoLayoutAttributes.frame.origin, size: autoLayoutSize)
        autoLayoutAttributes.frame = autoLayoutFrame
        return autoLayoutAttributes
    }
}
